package com.example.gybicycle.service;

import com.example.gybicycle.common.wxPay.WechatPayExternalService;
import com.example.gybicycle.entity.MemberDeposit;
import com.example.gybicycle.entity.MemberOrder;
import com.example.gybicycle.entity.MemberPay;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import com.wechat.pay.java.service.refund.model.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;

@Service
public class PayService {
    @Autowired
    private MemberDepositService depositService;
    @Autowired
    private MemberOrderService orderService;
    @Autowired
    private MemberPayService payService;
    @Autowired
    private WechatPayExternalService wxpayService;

    public void wxpay(Transaction transaction) {
        if (Transaction.TradeStateEnum.SUCCESS != transaction.getTradeState()) {

            if (transaction.getAttach().contains("ORDER")) {
                MemberOrder order = new MemberOrder();
                order.setOrderState(4);
                order.setId(transaction.getAttach());
                orderService.update(order);
            } else {
                MemberPay pay = new MemberPay();
                pay.setId(transaction.getOutTradeNo());
                pay.setPayState(2);
                pay.setPayTime(new Date());
                payService.update(pay);

                MemberDeposit deposit = new MemberDeposit();
                deposit.setId(transaction.getAttach());
                deposit.setDepositState(4);
                depositService.update(deposit);

                deposit = depositService.get(transaction.getAttach());
                if (deposit != null && deposit.getMemberOrderId() != null) {
                    MemberOrder order = new MemberOrder();
                    order.setOrderState(7);
                    orderService.update(order);
                }
            }

        } else {
//         支付成功
            MemberPay pay = new MemberPay();
            pay.setId(transaction.getOutTradeNo());
            pay.setPayState(1);
            pay.setPayTime(new Date());
            payService.update(pay);
            if (transaction.getAttach().contains("ORDER")) {
                refund(transaction.getAttach());
            } else {
                MemberDeposit deposit = new MemberDeposit();
                deposit.setId(transaction.getAttach());
                deposit.setDepositState(0);
                depositService.update(deposit);
                deposit = depositService.get(transaction.getAttach());
                if (deposit != null && deposit.getMemberOrderId() != null) {
                    MemberOrder order = new MemberOrder();
                    order.setOrderState(0);
                    orderService.update(order);
                }
            }
        }
    }

    public  void wxRefund(RefundNotification parse){
        try{
            System.out.println(parse);
            if (Status.SUCCESS.equals(parse.getRefundStatus())){
                MemberPay pay=new MemberPay();
                pay.setId(parse.getOutRefundNo());
                pay.setPayState(1);
                pay.setPayTime(new Date());
                payService.update(pay);
                pay=payService.get(parse.getOutRefundNo());
                MemberDeposit deposit=new MemberDeposit();
                deposit.setMemberOrderId(pay.getMemberOrderId());
                deposit.setDepositState(2);
                deposit.setRefundTime(new Date());
                depositService.updateByOrder(deposit);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public void refund(String orderId){
        MemberDeposit depospit=  depositService.getByNumber(orderId);
//                退款单
        MemberPay memberPay=new MemberPay();
        memberPay.setMoney(depospit.getMoney());
        memberPay.setPayType(2);
        memberPay.setPayState(0);
        memberPay.setMemberOrderId(depospit.getMemberOrderId());
        memberPay.setCreateTime(new Date());
        memberPay.setMemberId(depospit.getMemberId());
        payService.insert(memberPay);
//                待退订单
        MemberPay rePay=new MemberPay();
        rePay.setMemberOrderId(orderId);
        rePay.setPayType(0);
        rePay= payService.getOne(rePay);
        System.out.println("退款订单-"+rePay);
        System.out.println("退款单-"+memberPay.getId());
        Refund refund=wxpayService.refund(rePay,memberPay.getId());

        MemberDeposit deposit=new MemberDeposit();
        deposit.setMemberOrderId(orderId);
        deposit.setDepositState(1);
        depositService.update(deposit);

    }



}
